package design_patterns;

import java.io.*;

//深拷贝
public class Person  implements Cloneable , Serializable {

    private transient   int  age;
    private String name;

    public Person() {
    }

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person person = (Person) super.clone();
        person.age = this.age;
        person.name = this.name;
        return person;
    }

    public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
        Person person = new Person(18,"aaa");
        System.out.println(person);

        Person person1= (Person) person.clone();
        System.out.println(person1);

        Person person2=person;
        System.out.println(person2);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(person);

        ObjectInputStream objectInputStream=new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
        Person person3=(Person) objectInputStream.readObject();


        System.out.println(person3);
    }
}
